#version 410 core
struct Material
{
	// 漫反射贴图
	sampler2D   diffuse;
};

// 定向光源
struct DirectionLight
{
	vec3 direction;
	vec3 ambient;
	vec3 diffuse;
};

// 点光源
struct PointLight
{
	vec3 position;
	
	// 常数项
	float constant;
	// 线性衰减
    float linear;
	// 二次衰减 
    float quadratic;

	vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

struct Spotlight {
    vec3  position;
    vec3  direction;
    float cutOff;
    float outerCutOff;

	// 常数项
	float constant;
	// 线性衰减
    float linear;
	// 二次衰减 
    float quadratic;

	vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;
uniform Material material;
uniform DirectionLight light;
uniform Spotlight spotLight;

#define NR_POINT_LIGHTS 4
uniform PointLight pointLights[NR_POINT_LIGHTS];


vec3 calcDirLight(DirectionLight light, Material material, vec3 normal, vec2 texCoords);
vec3 calcPointLight(PointLight light, Material material, vec3 normal, vec3 fragPos,  vec2 texCoords);
vec3 calcSpotLight(Spotlight light, Material material, vec3 normal, vec3 fragPos, vec2 texCoords);


void main()
{
	vec3 norm = normalize(Normal);
	

	// 定向光
	vec3 result = calcDirLight(light, material, norm, TexCoords);

	// 点光源
	for(int i = 0; i < NR_POINT_LIGHTS; i++)
	{
		result += calcPointLight(pointLights[i], material, norm, FragPos, TexCoords);   
	}

	// 聚光灯
	result += calcSpotLight(spotLight, material, norm, FragPos,  TexCoords);

	FragColor = vec4(result, 1.0);
}

vec3 calcDirLight(DirectionLight light, Material material, vec3 normal, vec2 texCoords)
{
	vec3 lightDir = normalize(-light.direction);
	// 漫反射着色
	float diff = max(dot(normal, lightDir), 0.0);

	// 合并结果
	vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
	vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));

	return (ambient + diffuse);
}

vec3 calcPointLight(PointLight light, Material material, vec3 normal, vec3 fragPos, vec2 texCoords)
{
    vec3 lightDir = normalize(light.position - fragPos);
    // 漫反射着色
    float diff = max(dot(normal, lightDir), 0.0);

    // 衰减
    float distance    = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + 
                 light.quadratic * (distance * distance));    
    // 合并结果
    vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
    vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));

    ambient  *= attenuation;
    diffuse  *= attenuation;

	return (ambient + diffuse);
}

vec3 calcSpotLight(Spotlight light, Material material, vec3 normal, vec3 fragPos, vec2 texCoords)
{
	vec3 lightDir = normalize(light.position - fragPos);

	float theta = dot(lightDir, normalize(-light.direction));
	float epsilon   = light.cutOff - light.outerCutOff;
	float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);   
	   
	// 漫反射着色
	float diff = max(dot(normal, lightDir), 0.0);

	float distance = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); 

	// 合并结果
	vec3 ambient  = light.ambient  * vec3(texture(material.diffuse, texCoords));
	vec3 diffuse  = light.diffuse  * diff * vec3(texture(material.diffuse, texCoords));
  
	ambient *= attenuation * intensity;
    diffuse *= attenuation * intensity;
	return (ambient + diffuse);
}